import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;

import org.apache.poi.hssf.usermodel.HSSFCell;

public class DelTool {

	private static String dir_name = "E:\\workspace";
	boolean flag;

	public static void main(String[] args) throws Exception {
		// ReadHiddenFlor rd=new ReadHiddenFlor();
		// List filenames=rd.getFiles(dir_name);
		// for(int i =0;i<filenames.size();i++){
		// String str=(String)filenames.get(i);
		// System.out.println("第"+(i+1)+"文件名是："+str);
		// }
		File file = new File(dir_name);
		// deleteAllSvnFile(file);
		listFiles(file);
		// countFiles(dir_name);

		// calPriceBYYield();
		// System.err.println(digitToUpperCase("100000000.1"));
		//getFileByPath("C:\\Users\\zhuzc07996\\Desktop\\updatefiles.txt", "E");
		// getSQLS();
		String filePath = "‪c:\\1.xls";

		// readXls(filePath);

		LinkedList<String> linklist = new LinkedList<String>();
		linklist.add("b");
		linklist.add("a");
		linklist.add("d");
		linklist.add("c");
		linklist.add("f");
		linklist.add("d");

		System.err.println(linklist);

		Collections.sort(linklist);

		System.err.println(linklist);
		Collections.reverse(linklist);

		System.err.println(linklist);
		System.err.println(linklist.getLast());

		Date date = new Date();

		System.err.println("date = " + date);
		System.err.println("date.getTime = " + date.getTime());

		// date.setDate(20);

		String s = "20160305";
		Calendar ca = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
		ca.set(Calendar.YEAR, Integer.valueOf(s.substring(0, 4)));
		ca.set(Calendar.MONTH, Integer.valueOf(s.substring(4, 6)) - 1);
		ca.set(Calendar.DAY_OF_MONTH, Integer.valueOf(s.substring(6, 8)));
		System.err.println("date = " + ca.getTime());
		System.err.println("date.getTime = " + ca.getTime().getTime());

	}

	public List<String> getFiles(String dirname) {
		File dir = new File(dirname);
		File[] files = dir.listFiles();
		List<String> file_names = new ArrayList<String>();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {// 判断是否是目录
				file_names.add(files[i].getName() + "=>它是一个文件夹");
			}
			if (files[i].isHidden()) {// 判断是否是隐藏文件
				file_names.add(files[i].getName() + "=>它是一个隐藏文");
			}
			if (files[i].isFile() && (!files[i].isHidden())) {// 判断是否是文件并不能是隐藏文件
				file_names.add(files[i].getName());
			}
		}
		return file_names;
	}

	/**
	 * 删除文件目录下的svn标志文件
	 * 
	 * @Description
	 * @param file
	 * 
	 */
	public static void deleteAllSvnFile(File file) {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File fi : files) {
				deleteAllSvnFile(fi);
			}
		}
		if (file.getAbsolutePath().contains(".svn")) {
			System.out.println("删除  ---> " + file.getAbsolutePath());
			file.delete();
		}
	}

	public static void countFiles(String filePath) {
		String supportAutoclearBackupfilesRrNOT = "1";
		int backupfileRemaindays = 7;
		File file = new File(filePath);
		if ("1".equals(supportAutoclearBackupfilesRrNOT)) {
			if (file.isDirectory()) {
				String[] files = file.list();
				LinkedList<String> linklist = new LinkedList<String>();
				for (String filename : files) {
					linklist.add(filename);
				}
				Collections.sort(linklist);
				while (linklist.size() > backupfileRemaindays) {
					System.err.println(linklist);
					File f = new File(file.getAbsolutePath() + "\\" + linklist.getFirst());
					System.err.println(f);
					linklist.removeFirst();
					f.delete();
					System.err.println(linklist);
				}
			}
		}
	}

	public static void listFiles(File file) {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File fi : files) {
				listFiles(fi);
			}
		}
		if (file.getPath().contains(".")) {
			System.out.println("文件---> " + file.getPath());
		}
	}

	/**
	 * 国债预发行利率成交换算理论结算价格
	 * 
	 * @Description
	 * 
	 */
	public static void calPriceBYYield() {
		// 其中 C 为预发行国债发行票面利率，R为预发行交易成交收益率，f为年付息频率，n为预发行国债的期限
		double C = 0.0273;
		double R = 0.028;
		int f = 1;
		int n = 3;
		double price = 0;
		for (int i = 1; i <= f * n; i++) {
			price += (100 * C / f) / Math.pow((1 + R / f), i);
		}
		price += 100 / Math.pow((1 + R / f), f * n);
		System.err.println("price = " + price);
	}

	/**
	 * 复制文件到bank目录下
	 * 
	 * @Description
	 * @param sourceFiles
	 * @param desFilePath
	 * @throws Exception
	 * 
	 */
	public static void getFileByPath(String sourceFiles, String desFilePath) throws Exception {
		DataInputStream in = null;
		DataOutputStream out = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(sourceFiles)));
			String sourceFilePath = "";
			while ((sourceFilePath = reader.readLine()) != null) {
				if (sourceFilePath.trim() == "") {
					continue;
				}
				String toFile = sourceFilePath.replace(sourceFilePath.substring(0, sourceFilePath.indexOf(":")), desFilePath);
				File file = new File(toFile.substring(0, toFile.lastIndexOf("\\")));
				file.mkdirs();

				in = new DataInputStream(new BufferedInputStream(new FileInputStream(sourceFilePath)));
				out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(toFile, false)));
				while (in.available() != 0) {
					out.writeByte(in.readByte());
					out.flush();
				}
				System.err.println("文件复制完成--->" + toFile);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
	}

	public static String digitToUpperCase(String str) {
		if (Double.parseDouble(str) == 0.0) {
			return "零圆整";
		}
		String[] RMB = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		String[] unit = { "角", "分", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万" };

		String result = "";
		if (str.startsWith("-")) {
			result += "负";
			str = str.substring(1);
		}

		if (str.indexOf(".") < 0) {
			str += ".0";
		}
		String[] strArr = str.split("\\.");
		String str1 = strArr[0];
		String str2 = strArr[1];

		if (!"0".equals(str1)) {
			while (str1.charAt(0) == '0')
				str1 = str1.substring(1);
		}

		if (str2.length() > 2) {
			str2 = str2.substring(0, 2);
		}

		int length = str1.length();
		for (int i = 0; i < length; i++) {
			result += RMB[str1.charAt(i) - 48] + unit[length - i + 1];
		}

		length = str2.length();
		if (!"0".equals(str2) && !"00".equals(str2)) {
			for (int i = 0; i < length; i++) {
				result += RMB[str2.charAt(i) - 48] + unit[i];
			}
		}

		String[] regex = { "零角", "零拾", "零佰", "零仟" };
		for (int i = 0; i < regex.length; i++) {
			result = result.replace(regex[i], "零");
		}

		result = result.replace("零零零零万", "");
		result = result.replace("零零零零", "");
		result = result.replace("零零零", "零");
		result = result.replace("零零", "零");
		result = result.replace("零亿", "亿");
		result = result.replace("零万", "万");
		if (result.startsWith("零元")) {
			result = result.replace("零元", "");
		} else {
			result = result.replace("零元", "元");
		}
		result = result.replace("零角", "");
		result = result.replace("零分", "");

		if (result.endsWith("元") || result.endsWith("角")) {
			result += "整";
		} else if (result.endsWith("分")) {
			result += "";
		} else {
			result += "元整";
		}
		return result;
	}

	private static void getSQLS() throws SQLException {
		// String combiNo =
		// "BDAFS-RMB,BDAFS-RMBH,BDAFS-RMBO,BDAFS-RMBT,BDAFS-RSE,BDAFS-RSEO,BDAFS-RMG1,BDAFS-RMG2,BDAFS-LQDY,BDAFS-RMBB,BDHTM-RMB,BDHTM-RMBA,BDHTM-RMBO,BDHTM-RSE,BDHTM-RSEO,BDHTM-RMG1,BDHTM-RMG2,BDHTM-LQDY,BDLNR-RMB,BDLNR-RMBO,BDLNR-RSE,BDLNR-RMG1,BDLNR-RMG2,BDLNR-LQDY,BDTRD-RT01,BDTRD-RT02,BDTRD-RT03,BDTRD-RT04,BDTRD-RT05,BDTRD-RT06,BDTRD-RT07,BDTRD-RT08,BDTRD-RT09,BDTRD-RT10,BDTRD-RT11,BDTRD-RT12,BDTRD-RT13,BDTRD-RT14,BDTRD-RM01,BDTRD-RM02,BDTRD-RM03,BDTRD-RM04,BDTRD-RM05,BDTRD-RM06,BDTRD-RM07,BDTRD-RM08,BDTRD-RM09,BDTRD-RM10,
		// BDTRD-RD01,BDTRD-RD02,BDTRD-RD03,BDTRD-RD04,BDTRD-RD05,BDTRD-RD06,BDTRD-RD07,BDTRD-RD08,BDTRD-RD09,BDTRD-RD10";
		String combiNoSQL = "insert into TBCOMBI (combi_no, fund_no, combi_name, branch_no, asset_no, invest_type, combi_class, param_id, setup_date, end_date, combi_status, stock_type_right, entrdire_right, entrust_branch_no) values ('name111', '自营', 'desc111', '000000', 'foldername', ' ', '1', 0, 20150919, 0, '1', ' ', ' ', ' ');";
		String TBCOMBISEATS = "insert into TBCOMBISEATS (combi_no, sh_seat_no, sz_seat_no, reserve) values ('test', '30029', '709412', ' ');";
		String combiOPRight1 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('songz', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight2 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('weixn', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight3 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('wangjy', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight4 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('xuzz', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight5 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('wangyq', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight6 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('qianbk', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight7 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('wuhw', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight8 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('zhuy', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight9 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('wuwei', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight10 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('cuiyd', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight11 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('xuann', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight12 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('wusm', '3', ' ', ' ', 'test', '2', '1');";
		String combiOPRight13 = "insert into TBOPFUNDCOMBIRIGHT (user_id, layer, fund_no, asset_no, combi_no, rights, status) values ('wangxh', '3', ' ', ' ', 'test', '2', '1');";
		BaseDAO bd = new BaseDAO();
		String sql = "select * from test";
		ResultSet rs = bd.exeuteQuery(sql, null);
		int count = 0;
		while (rs.next()) {
			String combiname = rs.getString(1);
			String desc = rs.getString(2);
			System.err.println(combiNoSQL.replace("name111", combiname).replace("desc111", desc).replace("foldername", combiname));
			System.err.println(TBCOMBISEATS.replace("test", combiname));
			// System.err.println(combiOPRight1.replace("test", combiname));
			// System.err.println(combiOPRight2.replace("test", combiname));
			// System.err.println(combiOPRight3.replace("test", combiname));
			// System.err.println(combiOPRight4.replace("test", combiname));
			// System.err.println(combiOPRight5.replace("test", combiname));
			// System.err.println(combiOPRight6.replace("test", combiname));
			// System.err.println(combiOPRight7.replace("test", combiname));
			// System.err.println(combiOPRight8.replace("test", combiname));
			// System.err.println(combiOPRight9.replace("test", combiname));
			// System.err.println(combiOPRight10.replace("test", combiname));
			// System.err.println(combiOPRight11.replace("test", combiname));
			// System.err.println(combiOPRight12.replace("test", combiname));
			// System.err.println(combiOPRight13.replace("test", combiname));
			count += 1;
		}
		System.err.println(count);
	}

	public static void readXls(String path) throws IOException, Exception, SQLException {
		// Read the Sheet
		String unitstocksql = "insert into TBUNITSTOCK (TRANS_DATE, FUND_NO, ASSET_NO, COMBI_NO, INVEST_TYPE, INTER_CODE, MARKET_NO, BEGIN_AMOUNT, CURRENT_AMOUNT, FROZEN_AMOUNT, UNFROZEN_AMOUNT, BUY_AMOUNT, SALE_AMOUNT, PRE_BUY_AMOUNT, PRE_SALE_AMOUNT, TEMP_FROZEN_AMOUNT, TEMP_FROZEN_AMOUNT2, TEMP_UNFROZEN_AMOUNT, T1_TEMP_FROZEN_AMOUNT, T1_FROZEN_AMOUNT, BUY_BALANCE, SALE_BALANCE, BUY_FEE, SALE_FEE, BUY_UNSETTLE_AMOUNT, SALE_UNSETTLE_AMOUNT, BUY_UNSETTLE_BALANCE, SALE_UNSETTLE_BALANCE, BUY_UNSETTLE_AMOUNT2, SALE_UNSETTLE_AMOUNT2, UNTRANSFERED_INVEST, FAIRVALUE_CHANGE, INTEREST_INVEST, ACCUMULATE_PROFIT, TURN_INVEST, TURN_INTEREST_INVEST, TURN_PROFIT, SEAT_NO, STOCKHOLDER_ID, POSITION_FLAG, TODAY_PROFIT, CURRENT_COST, PRE_IMPAWN_AMOUNT, IMPAWN_AMOUNT, PRE_RETURN_AMOUNT, RETURN_AMOUNT, CURRENT_IMPAWN_AMOUNT) values (20160803, '自营', ' ', 'combiname', 'investtype', 'intercode', '6', 0, 'currentamount', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '30029', 'B882720074', '1', 0, 0, 0, 0, 0, 0, 0);";

		// String pricessql =
		// "insert into TBUNITSTOCKINVESTYEARINIT (trans_date, market_no,
		// fund_no, combi_no, invest_type, inter_code, cost_value, yf_interest,
		// sell_interest, buy_interest, total_interset, invest_cost, cost_price,
		// capital_gains, profit, sellprice_income) values (20150921, '6', '自营',
		// 'combiname', 'investtype', 'intercode', 0, 0, 0, 0, 0, 0,
		// 'aaaaprice', 0, 0, 0);";

		String ftpsql = "insert into TBFTPRATE (trans_date, invest_type, combi_no, inter_code, ftp_rate, rate_type) values (20160803, 'investtype', 'combiname', 'intercode', 0, ' ');";

		String fundsql = "insert into TBFUNDSTOCK (TRANS_DATE, FUND_NO, INVEST_TYPE, INTER_CODE, MARKET_NO, BEGIN_AMOUNT, CURRENT_AMOUNT, BUY_AMOUNT, SALE_AMOUNT, PRE_BUY_AMOUNT, PRE_SALE_AMOUNT, BUY_BALANCE, SALE_BALANCE, BUY_FEE, SALE_FEE, BUY_UNSETTLE_AMOUNT, SALE_UNSETTLE_AMOUNT, BUY_UNSETTLE_BALANCE, SALE_UNSETTLE_BALANCE, BUY_UNSETTLE_AMOUNT2, SALE_UNSETTLE_AMOUNT2, TEMP_FROZEN_AMOUNT, TEMP_FROZEN_AMOUNT2, TEMP_UNFROZEN_AMOUNT, UNTRANSFERED_INVEST, FAIRVALUE_CHANGE, INTEREST_INVEST, ACCUMULATE_PROFIT, TURN_INVEST, TURN_INTEREST_INVEST, TURN_PROFIT, CURRENT_COST, TODAY_PROFIT, WEIGHTED_PROFIT) values (20160803, '自营', 'investtype', 'intercode', '6', 0, 'currenta', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);";

		// String selectsql =
		// "select test1.combiname,test1.intercode,test1.amount,test1.investtype
		// from test1";
		String selectsql = "SELECT NAME,DES,INVESTTYPE FROM BFM20.TEST";
		BaseDAO bd = new BaseDAO();
		ResultSet rs = bd.exeuteQuery(selectsql, null);
		while (rs.next()) {
			// String combiname = rs.getString(1);
			// String intercode = rs.getString(2);
			// String amount = rs.getString(3);
			// String investtypetemp = rs.getString(4);
			String combiname = rs.getString(1);
			String investtypetemp = rs.getString(3);

			String investtype = "1";

			if (investtypetemp.equals("可供出售")) {
				investtype = "3";
				System.err.println(ftpsql.replace("investtype", investtype).replace("combiname", combiname).replace("intercode", " "));
			} else if (investtypetemp.equals("可交易")) {
				investtype = "1";
				System.err.println(ftpsql.replace("investtype", investtype).replace("combiname", combiname).replace("intercode", " "));
			} else if (investtypetemp.equals("贷款和应收款项")) {
				investtype = "4";
				// System.err.println(ftpsql.replace("investtype",
				// investtype).replace("combiname",
				// combiname).replace("intercode", intercode));
			}
			// Double damount = BigDecimalUtil.div(Double.valueOf((amount)),
			// 100);
			//
			// System.out.println(unitstocksql.replace("combiname",
			// combiname).replace("intercode",
			// intercode).replace("currentamount", String.valueOf(damount))
			// .replace("investtype", investtype));// combiname

			// System.err.println(fundsql.replace("investtype",
			// investtype).replace("intercode", intercode).replace("currenta",
			// String.valueOf(damount)));
		}
	}


	private static String getValue(HSSFCell hssfCell) {
		if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(hssfCell.getBooleanCellValue());
		} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(hssfCell.getNumericCellValue());
		} else {
			return String.valueOf(hssfCell.getStringCellValue());
		}
	}

}